package edu.ciee.app.jichuxinxi.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.app.action.util.JdbcUtil;
import edu.ciee.app.action.util.SessionUtil;

public class YuangongAction {

	/*
	 * ID int auto_increment, name varchar(20), sex varchar(2), birthday
	 * Datetime, zhibie int, tele varchar(20),
	 */
	private int id;
	private String name;
	private String sex;
	private String birthday;
	private int zhibie;
	private String tele;
	private String tip;
	private List resultList;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public int getZhibie() {
		return zhibie;
	}

	public void setZhibie(int zhibie) {
		this.zhibie = zhibie;
	}

	public String getTele() {
		return tele;
	}

	public void setTele(String tele) {
		this.tele = tele;
	}

	public String insert() throws Exception {


		String sql = "insert into yg_table(name,sex,birthday,zhibie,tele) values(\'"
				+ this.getName()
				+ "\',"
				+ "\'"
				+ this.getSex()
				+ "\',"
				+ "\'"
				+ this.getBirthday()
				+ "\',"
				+ "\'"
				+ this.getZhibie()
				+ "\',\'" + this.getTele() + "\');";
		
		System.out.println(sql);
		
		try{
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("yuangongmap");
			SessionUtil.updateSession("YG_table", "name", "yuangongmap","yzsystem");
			
			this.setTip("Success.");
			return "refresh";
		}catch(Exception e){
			this.setTip(e.toString() + this.getTip());
			return "error";			
		}
	}

	public String search() {
		System.out.println("Birthday="+this.getBirthday());
		String query = "where 1=1";

		if (this.getId() != 0) {
			query = query + " and ID like '%" + this.getId() + "%'";// this.getID()
		}
		if (this.getName() != null) {
			query = query + " and yg_table.name like '%" + this.getName() + "%'"; // this.getName()
		}
		if (this.getSex() != null) {
			query = query + " and sex like '%" + this.getSex() + "%'"; // this.getName()
		}
		if (this.getBirthday() != null) {
			query = query + " and birthday like '%" + this.getBirthday() + "%'"; // this.getInfo()
		}
		if (this.getZhibie() != 0) {
			query = query + " and zhibie like '%" + this.getZhibie() + "%'"; // this.getName()
		}
		if (this.getTele() != null) {
			query = query + " and tele like '%" + this.getTele() + "%'"; // this.getName()
		}
		
		String sql = "select  yg_table.ID as yg_id,yg_table.name as yg_name,sex,birthday,zhibie,tele,zhibie_table.name as zhibie_name " +
				" from yg_table,zhibie_table " + query+" and yg_table.zhibie=zhibie_table.ID";
		System.out.println(sql);
		try {
			
			
			resultList = JdbcUtil.searchResult(sql,"yzsystem");
			return "success";
		} catch (Exception e) {
			this.setTip(e.toString());
			return "error";
		}
	}

	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		Map m = ctx.getParameters();
		String id = request.getParameter("id");
		System.out.println(id);
		String sql = "delete from yg_table where ID=" + id + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("yuangongmap");
			SessionUtil.updateSession("YG_table", "name", "yuangongmap","yzsystem");

			return "refresh";
		} catch (Exception e) {
			this.setTip("error" + e.toString());
			return "error";
		}

	}

	public String forEdit() {
		
		System.out.println("name="+name);
		System.out.println("sex="+sex);

		return "success";
	}

	public String edit() {

		String sql = "update yg_table set name = \'" + this.getName()
				+ "\',sex=\'" + this.getSex() 
				+ "\',zhibie="+ this.getZhibie()
				+ ",birthday=\'"+ this.getBirthday()
				+ "\',tele=\'"+ this.getTele()
				+ "\' where ID=" + this.getId() + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("yuangongmap");
			SessionUtil.updateSession("YG_table", "name", "yuangongmap","yzsystem");

			return "refresh";
		} catch (Exception e) {
			return "error";
		}
	}

}
